package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;


    @RequestMapping(value = "/list",name = "展示菜单列表数据")
    public String findAll(@RequestParam(name="page",defaultValue = "1") Integer pageNum,
                         @RequestParam(name="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Module> pageInfo = moduleService.findPage(pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这个 “page” 不能写错，因为页面就是取的是page中的值
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到新增菜单的页面")
    public String toAdd( ){
//        查询所有的菜单数据，放入到request中
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/edit",name = "保存菜单方法")
    public String save(Module module){
//        新增 id上是没有值的
//        修改 id是有值的
        if(StringUtils.isEmpty(module.getId())){ //ID为空 新增
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }else{
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do"; //重定向到菜单列表中
    }

    @RequestMapping(value = "/toUpdate",name = "进入到菜单编辑页面")
    public String toUpdate(String id){
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);

        //        查询所有的菜单数据，放入到request中
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);

        return "system/module/module-add";
    }
    @RequestMapping(value = "/delete",name = "删除菜单数据")
    public String delete(String id){
        moduleService.deleteById(id);
        return "redirect:/system/module/list.do";
    }


}
